import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',//方法一 localStorage.getItem("token")
    //反序列化获取session会话中的 userInfo对象
    userInfo:JSON.parse(sessionStorage.getItem("userInfo")),
    longitude:JSON.parse(localStorage.getItem("longitude")),
    latitude:JSON.parse(localStorage.getItem("latitude")),
    city:JSON.parse(localStorage.getItem("city")),
    waypoints:JSON.parse(localStorage.getItem("waypoints")),
    linearr:JSON.parse(localStorage.getItem("linearr"))
  },
  getters: {
    //get
    //配置一个getUser可以直接获取已经反序列化对象的一个userInfo
    getUser: state=>{
      return state.userInfo;
    },
    getToken: state=>{
      return state.token;
    },
    getPlng: state => {
      return state.longitude;
    },
    getPLat:state => {
      return state.latitude;
    },
    getCity:state => {
      return state.city;
    },
    getWaypoints:state => {
      return state.waypoints
    },
    getLinearr:state => {
      return state.linearr
    }
  },
  mutations: {
    //set
    SET_TOKEN:(state,token)=>{
      state.token=token//将传入的token赋值 给state的token
      //同时可以存入浏览器的localStorage里面
      localStorage.setItem("token",token)
    },
    SET_USERINFO:(state,userInfo)=>{
      state.userInfo=userInfo//将传入的tuserInfo赋值 给state的userInfo
      //同时可以存入会话的sessionStorage里面 sessionStorage中只能存字符串 不能存入对象所以我们存入序列化 jons串
      sessionStorage.setItem("userInfo",JSON.stringify(userInfo))
    },
    SET_LONGITUDE:(state,plng)=>{
      state.plng=plng//将传入的plug赋值 给state的plug
      //同时可以存入会话的sessionStorage里面 sessionStorage中只能存字符串 不能存入对象所以我们存入序列化 jons串
      localStorage.setItem("longitude",JSON.stringify(plng))
    },
    SET_LATITUDE:(state,plat)=>{
      state.plat=plat//将传入的plat赋值 给state的plat
      //同时可以存入会话的sessionStorage里面 sessionStorage中只能存字符串 不能存入对象所以我们存入序列化 jons串
      localStorage.setItem("latitude",JSON.stringify(plat))
    },
    SET_CITY:(state,city)=>{
      state.city=city//将传入的city赋值 给state的city
      //同时可以存入会话的sessionStorage里面 sessionStorage中只能存字符串 不能存入对象所以我们存入序列化 jons串
      localStorage.setItem("city",JSON.stringify(city))
    },
    SET_WAYPOINTS:(state,waypoints )=> {
      state.waypoints=waypoints
      localStorage.setItem("waypoints",JSON.stringify(waypoints))
    },
    SET_LINEARR:(state ,linearr)=>{
      state.linearr=linearr
      localStorage.setItem("linearr",JSON.stringify(linearr))
    },

    //删除token及userInfo
    REMOVE_INFO:(state)=>{
      state.token = '';
      localStorage.setItem("token",'')
      state.userInfo = {};
      sessionStorage.setItem("userInfo",JSON.stringify(''))

    },
    REMOVE_WAY:(state)=>{
      //删除员工的经纬度和目的地经纬度
      state.longitude={};
      state.latitude={};
      state.city={};
      state.waypoints={};
      localStorage.setItem("longitude",JSON.stringify(''))
      localStorage.setItem("latitude",JSON.stringify(''))
      localStorage.setItem("city",JSON.stringify(''))
      localStorage.setItem("waypoints",JSON.stringify(''))
    },
    REMOVE_ARR:(state )=>{
      state.linearr={};
      localStorage.setItem("linearr",JSON.stringify(''))
    }
  },
  actions: {
  },
  modules: {
  }
})
